import encodeParams from '@/utils/encodeParams'
import { type Ref, ref, watch } from 'vue'
const monitorArr: { url: string | null; fun: () => void }[] = []
// 不做逻辑判断，全部清理
const clearPageDataForUrl = (clearUrl: string | null) => {
  if (clearUrl == null) return
  for (const obj of monitorArr) {
    if (clearUrl === '*' || clearUrl === obj.url) {
      obj.fun()
    }
  }
}
// 关闭所有页面，打开到应用内的某个页面
uni.addInterceptor('reLaunch', {
  invoke: () => clearPageDataForUrl('*')
})
// 关闭当前页面，跳转到应用内的某个页面
uni.addInterceptor('redirectTo', {
  invoke: () => clearPageDataForUrl('*')
})
uni.addInterceptor('navigateBack', {
  invoke: () => {
    // 当页面数据与当前路由相同时，清理
    clearPageDataForUrl(getCurrentPageUrl())
  }
})
function getCurrentPageUrl() {
  const curPage = getCurrentPages()
  return curPage[curPage.length - 1].route ?? null //获取当前页面的路由
}
export class PageRefVar<T> {
  private value = ref<T | undefined>()
  private url: string | null = null
  private onChangeMonitor: ((value: T | undefined) => void)[] = []
  public constructor(value?: T | undefined) {
    this.value.value = value
    monitorArr.push({ url: this.url, fun: () => (this.value.value = undefined) })
    watch(
      () => this.value.value,
      () => this.runOnChangeMonitorFun()
    )
  }
  public watch(fun: (value: T | undefined) => void, isImmediate = false) {
    this.onChangeMonitor.push(fun)
    if (isImmediate) fun(this.value.value)
    return () => {
      // 移除监听
      for (const [index, inFun] of this.onChangeMonitor.entries()) {
        if (fun === inFun) this.onChangeMonitor.splice(index, 1)
      }
    }
  }
  private runOnChangeMonitorFun() {
    for (const fun of this.onChangeMonitor) {
      fun(this.value.value)
    }
  }
  public getRef(): Ref<T | undefined> {
    return this.value
  }
  public setValue(t: T | undefined) {
    // this.checkAndUpdateIsPageKeyChange()
    this.url = getCurrentPageUrl()
    this.value.value = t
    // this.runOnChangeMonitorFun()
  }
  public getValue(): T | undefined {
    return this.value.value
  }
}
// /**
//  * Ref形式的PageData
//  */
// export class PageRefVar<T> {
//   private value = new PageData<Ref<T | null | undefined>>(ref(null))
//   private url: string | null = null
//   private onChangeMonitor: ((value: T | null | undefined) => void)[] = []
//   public constructor(value?: T) {
//     this.value = value
//     monitorArr.push({ url: this.url, fun: () => (this.value = null) })
//   }
//   public get(): Ref<T | null | undefined> {
//     return this.t.getValue()!
//   }
//   public getValue(): T | null | undefined {
//     return this.t.getValue()?.value
//   }
//   public setValue(t: T | null | undefined) {
//     this.t.getValue()!.value = t
//   }
//   public onChange(fun: (value: T | null | undefined) => void, isImmediate = false) {
//     this.onChangeMonitor.push(fun)
//     if (isImmediate) fun(this.value)
//     return () => {
//       // 移除监听
//       for (const [index, inFun] of this.onChangeMonitor.entries()) {
//         if (fun === inFun) this.onChangeMonitor.splice(index, 1)
//       }
//     }
//   }
// }
